/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mail;

import java.util.HashMap;
import java.util.Map;
import javax.mail.internet.MimeMessage;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;

/**
 *
 * @author tomasz
 */
public class sendInvitation {
    
    
   private JavaMailSender mailSender;
   
   private VelocityEngine velocityEngine;

   @Autowired
   public void setMailSender(JavaMailSender mailSender) {
      this.mailSender = mailSender;
   }

   @Autowired
   public void setVelocityEngine(VelocityEngine velocityEngine) {
      this.velocityEngine = velocityEngine;
   }
   
    public void sendConfirmationEmail() 
    {
        
        System.out.print("--POSZLO--");
         MimeMessagePreparator preparator = new MimeMessagePreparator() {
                 public void prepare(MimeMessage mimeMessage) throws Exception {
                     
                    MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                    
                    message.setSubject("Poszło");
                    message.setTo("apap001@wp.pl");
                    message.setFrom("apap001@wp.pl"); // could be parameterized...
                    
                    User user = new User();
                    user.setEmailAddress("poszlo");
                    
                    Map model = new HashMap();
                    model.put("firma", "dd");
                    
                    String text = VelocityEngineUtils.mergeTemplateIntoString(
                                            velocityEngine, "/invitation.vm", model);
                    
                    message.setText(text, true);
                    
                 }
         };

      
      this.mailSender.send(preparator);
      
     
      
   }
}
